package org.gnyxy.controller.front;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.dto.OrdersPaymentDTO;
import org.gnyxy.dto.OrdersSubmitDTO;
import org.gnyxy.exception.BaseException;
import org.gnyxy.result.PageResult;
import org.gnyxy.result.Result;
import org.gnyxy.service.OrdersService;
import org.gnyxy.vo.OrderPaymentVO;
import org.gnyxy.vo.OrderVO;
import org.gnyxy.vo.OrdersSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrdersController")
@RequestMapping("/front/api/orders")
@Slf4j
@Api(tags = "小程序端-订单接口")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    
    @PostMapping("/submit")
    @ApiOperation("提交订单")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("提交订单 :{}",ordersSubmitDTO);
        OrdersSubmitVO ordersSubmitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(ordersSubmitVO);
    }

    @PostMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付: {}",ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
    @GetMapping("/getAll")
    @ApiOperation("查询历史订单")
    public Result<PageResult> getAll(Integer pageNum,Integer pageSize,Integer status){
        PageResult pageResult = ordersService.PageQuery(pageNum,pageSize,status);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        OrderVO orderVO = ordersService.details(id);
        return Result.success(orderVO);
    }

    /**
     * 用户取消订单
     *
     * @return
     */
    @PostMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable("id") Long id) throws Exception {
        ordersService.userCancelById(id);
        return Result.success();
    }
}
